
#include "function.h"
#include <cassert>
#include <fstream>
#include <iomanip>
#include <iostream>
using namespace std;
void OutputResult(const VarArray<Variable, 2, 0> &flow,
                  const VarArray<Point, 2, 0> &grid, ostream &os);
constexpr double re_num{10000};

int main() {
  VarArray<Point, 2, 0> grid;
  GridGenerate(grid, 300, 200);
  OutPutGridInPlot3d(grid);
//   exit(EXIT_FAILURE);
  const int np_in_xdir{grid.Dim(0)};
  const int np_in_ydir{grid.Dim(1)};
  VarArray<Variable, 2, 0> flow(np_in_xdir, np_in_ydir);

  InitializeFlow(flow);

  // 能正常运行， 但 v 不太好， 其它符合的挺好
  CalcUVsecondOrder updatUV(flow, grid, re_num);

  //   CalcUVsecondOrder2 updatUV(flow, grid, re_num);

  for (int j = 1; j < np_in_xdir; ++j) {
    InitializeNextLine(flow, j);
    // cout << "hello" << endl;
    updatUV.IterateForUV(j);
  }

  ofstream fluid{"flow.plt"};
  OutputResult(flow, grid, fluid);
  fluid.close();

  return 0;
}

void OutputResult(const VarArray<Variable, 2, 0> &flow,
                  const VarArray<Point, 2, 0> &grid, ostream &os) {
  int npoint_in_ydir = flow.Dim(1);
  int npoint_in_xdir = flow.Dim(0);

  os << "TITLE = \" visualization of the 2Dflow\" \n"
     << "VARIABLES = \"x\", \"y\", \"u\", \"v\"\n";

  os << "ZONE  I=" << npoint_in_xdir << ", J=" << npoint_in_ydir
     << ", DATAPACKING=POINT\n";

  os << std::scientific << std::setprecision(16);

  for (int j = 0; j < npoint_in_ydir; ++j) {
    for (int i = 0; i < npoint_in_xdir; ++i) {
      os << grid(i, j).x << " " << grid(i, j).y << " " << flow(i, j).u << " "
         << flow(i, j).v << "\n";
    }
  }

  // string last{"myBlasius.plt"};
  ofstream blasius{"myBlasius.plt"};
  blasius << std::scientific << std::setprecision(16);
  blasius << "TITLE = \" visualization of the last line\" \n"
          << "VARIABLES = \"y\", \"u\", \"v\", \"eta\"\n";

  blasius << "ZONE  I=" << npoint_in_ydir << endl;

  for (int j = 0; j < npoint_in_ydir; ++j) {
    const Point &p = grid(npoint_in_xdir - 1, j);
    blasius << p.y << " " << flow(npoint_in_xdir - 1, j).u << " "
            << flow(npoint_in_xdir - 1, j).v << " "
            << p.y / p.x * sqrt(re_num * p.x) << endl;
  }
  blasius.close();
}